package com.trimps.wifi.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.trimps.wifi.exception.LoginException;
import com.trimps.wifi.exception.SqlOperationsException;
import com.trimps.wifi.mapper.UserMapper;
import com.trimps.wifi.pojo.User;
import com.trimps.wifi.service.IUserService;

@Service("userService")
public class UserServiceImpl implements IUserService {
	@Autowired
	private UserMapper userMapper;	


	@Override
	public List<User> getAllUser(int currentPage, int rowPerPage) {
		return userMapper.getAllUser(currentPage, rowPerPage);
	}

    @Override
    public int getUserCount() {
        return userMapper.getUserCount();
    }

    @Override
    public User login(String username, String password) throws LoginException {
        User user = new User();
        user.setUserName(username);
        user.setPassword(password);
        return userMapper.getUser(user);
    }

    @Override
    public User getUserByUserName(String username) {
        User user = new User();
        user.setUserName(username);
        return userMapper.getUser(user);
    }

    @Override
    public int delById(int id) {
        return userMapper.delById(id);
    }

    @Override
    public int save(User user) throws SqlOperationsException {
        User rtUser = getUserByUserName(user.getUserName());
        if(rtUser != null && rtUser.getId() != 0){
            throw new SqlOperationsException("该用户名已经存在，不能重复添加。");
        }
        return userMapper.save(user);
    }

    @Override
    public int update(User user) {
        return userMapper.update( user);
    }
}
